package com.zhanhong.opencvdemo.view;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

import com.zhanhong.opencvdemo.R;
import com.zhanhong.opencvdemo.dialog.SimpleProgressDialog;
import com.zhanhong.opencvdemo.presenter.CropPresenter;
import com.zhanhong.opencvdemo.presenter.CropPresenterImpl;
import com.zhanhong.opencvdemo.utils.ToastUtils;

import me.pqpo.smartcropperlib.view.CropImageView;

public class CropActivity extends AppCompatActivity {

    private CropPresenter mPresenter;
    private CropImageView mIvCrop;
    private TextView mTvCancel;
    private TextView mTvConfirm;
    private SimpleProgressDialog mProgressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_crop);
        initData();
        initView();
    }

    private void initData() {
        mPresenter = new CropPresenterImpl(this);
    }

    private void initView() {
        mIvCrop = findViewById(R.id.iv_crop);
        mTvCancel = findViewById(R.id.tv_exit);
        mTvConfirm = findViewById(R.id.tv_confirm);
        mTvCancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setResult(RESULT_CANCELED);
                finish();
            }
        });
        mTvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                if (mIvCrop.canRightCrop()) {
                    mPresenter.crop(mIvCrop);
                } else {
                    ToastUtils.showToast(CropActivity.this, "初始化失败");
                    finish();
                }
            }
        });
        mPresenter.analyzeSrcBitmap(getIntent().getStringExtra("filePath"));
    }

    private void showProgress(String content) {
        if (mProgressDialog == null) {
            mProgressDialog = new SimpleProgressDialog(this);
        }
        mProgressDialog.show();
        mProgressDialog.setContent(content);
    }

    private void hideProgress() {
        if (mProgressDialog != null) {
            mProgressDialog.dismiss();
        }
    }

    public void onAnalyzeSrcBitmapBegin() {
        showProgress("Bitmap解码");
    }

    public void onAnalyzeSrcBitmapComplete(Bitmap bitmap, Point[] points) {
        hideProgress();
        mIvCrop.setImageToCrop(bitmap);
        mIvCrop.setCropPoints(points);
    }

    public void onCropBegin() {
        showProgress("图片裁剪及分析");
    }

    public void onCropComplete(String cropPath) {
        hideProgress();
        Intent intent = new Intent();
        intent.putExtra("cropPath", cropPath);
        setResult(RESULT_OK, intent);
        finish();
    }

    public void onCropError(Exception e) {
        setResult(RESULT_CANCELED);
        ToastUtils.showToast(this, e.getMessage());
        finish();
    }
}
